<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/js/layui/css/layui.css" media="all">


</head>
<body>
<br>
<form class="layui-form" action="">
    <%--进货数量--%>
    <div class="layui-form-item">
        <label class="layui-form-label">进货数量<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="inNum" style="width: 350px" name="username" value="" lay-verify="required|integer" lay-reqtext="进货数量是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--进货单价--%>
    <div class="layui-form-item">
        <label class="layui-form-label">进货单价<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="inPri" style="width: 350px" name="username" lay-verify="required|price" lay-reqtext="进货单价是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--供应商--%>
    <div class="layui-form-item">
        <label class="layui-form-label">供应商<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="supplier" style="width: 350px" name="schAddress" lay-verify="required" lay-reqtext="供应商是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--表单提交--%>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${pageContext.request.contextPath}/assets/js/core/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/layui/layui.js"></script>


<script>
    var goId = ${param.goId}; //获得 goodsManager.jsp页面传递过来的参数值
    var accId = ${param.accId};

    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义表单验证规则 （input输入框中的属性 lay-verify 的值与下面对应）
        // required 内置的校验 输入框内容不能为空
        //title 指标题验证，pass 指密码验证，可以自定义正则表达式，confirmPass 指两次密码相同验证
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [            //定义密码的正则表达式
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
            ,confirmPass:function(value){
                if($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
            ,price: [           //定义商品价格的正则表达式
                /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
                ,'进货单价必须是正整数或者保留两位小数'
            ]
            ,integer: [
                /^[1-9]\d*$/
                ,'进货数量必须是正整数'
            ]
        });

        //监听表单提交
        form.on('submit(demo1)', function(data){
            layer.confirm('确定进货吗？', function(index){ //弹出确定提交弹窗
                var param = {
                  goId:goId,
                  inNum: $("#inNum").val(),
                  inPri: $("#inPri").val(),
                  supplier: $("#supplier").val(),
                  accId:accId
                }
                /*发送商品进货请求*/
                var url = "/goods/inGoods";
                $.post(url,param,function (data){
                    if(data.code == 1001){
                    //    layer.msg(data.msg);
                        layer.closeAll();       //关闭确认提交的弹出
                        layer.open({
                            type: 1
                            ,content: '<div style="padding: 20px 100px;">'+ data.msg +'</div>'
                            ,btn: '确定'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                            //    layer.closeAll();
                                top.location='goodsManager.jsp';  //点击确定后跳转到 商品信息管理页面
                            }
                        });

                    }else if(data.code == 1002){
                        layer.msg(data.msg);
                    }
                })
            });

            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

    });
</script>
</body>
</html>
